package com.cruzr.opengles.map.layer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;

import com.cruzr.opengles.R;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class ImageLayer {

    protected FloatBuffer mPositionBuffer;
    protected FloatBuffer mCoordinateBuffer;
    protected Bitmap mBitmap;

    protected int mTextureId;

    protected float[] mCoordinate = {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 0.0f,
            1.0f, 1.0f,
    };

    private float[] mPoints = {
            -0.2f, 0.2f,    //左上角
            -0.2f, -0.2f,   //左下角
            0.2f, 0.2f,     //右上角
            0.2f, -0.2f     //右下角
    };

    public ImageLayer(Context context) {
        ByteBuffer positionBuffer = ByteBuffer.allocateDirect(mPoints.length * 4);
        positionBuffer.order(ByteOrder.nativeOrder());
        mPositionBuffer = positionBuffer.asFloatBuffer();
        mPositionBuffer.put(mPoints);
        mPositionBuffer.position(0);

        ByteBuffer coordinateBuffer = ByteBuffer.allocateDirect(mCoordinate.length * 4);
        coordinateBuffer.order(ByteOrder.nativeOrder());
        mCoordinateBuffer = coordinateBuffer.asFloatBuffer();
        mCoordinateBuffer.put(mCoordinate);
        mCoordinateBuffer.position(0);

        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.cruzr, null);
    }


    public int createTexture() {
        int[] texture = new int[1];
        if (mBitmap != null && !mBitmap.isRecycled()) {
            GLES20.glGenTextures(1, texture, 0);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, mBitmap, 0);
        }
        recycleBitmap();
        mTextureId = texture[0];
        return mTextureId;
    }

    public void drawMap(int glPosition, int glCoordinate) {
        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
        GLES20.glEnableVertexAttribArray(glPosition);
        GLES20.glEnableVertexAttribArray(glCoordinate);
        GLES20.glVertexAttribPointer(glPosition, 2, GLES20.GL_FLOAT, false, 0, mPositionBuffer);
        GLES20.glVertexAttribPointer(glCoordinate, 2, GLES20.GL_FLOAT, false, 0, mCoordinateBuffer);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureId);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        GLES20.glDisableVertexAttribArray(glPosition);
        GLES20.glDisableVertexAttribArray(glCoordinate);
        GLES20.glDisable(GLES20.GL_BLEND);
    }

    public void recycleBitmap() {
        if (mBitmap != null && !mBitmap.isRecycled()) {
            mBitmap.recycle();
            mBitmap = null;
        }
    }

    public void onDestroy() {
        recycleBitmap();
        GLES20.glDeleteTextures(1, new int[]{mTextureId}, 0);
    }
}
